package com.wangsd.common.utils;

import com.wangsd.web.sys.entity.SysButton;
import io.swagger.models.auth.In;
import org.springframework.web.util.UriUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ApplicationUtils : 程序工具类，提供大量的便捷方法
 */
public class ApplicationUtils {


    /**
     * 将16进制的byte数组转换成字符串
     *
     * @param raw
     * @return
     */
    public static String getBufHexStr(byte[] raw) {
        String HEXES = "0123456789ABCDEF";
        if (raw == null) {
            return null;
        }
        final StringBuilder hex = new StringBuilder(2 * raw.length);
        for (final byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }

    /**
     * 将16进制的字符串转成字符数组
     *
     * @param str
     * @return
     */
    public static byte[] getHexBytes(String str) {
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }

    /**
     * url 编码
     *
     * @param source url
     * @return 编码后的url
     */
    public static String encodeURL(String source) {
        return UriUtils.encode(source, "utf-8");
    }

    public static Long getButtonId(List<SysButton> buttonlist, String action) {
        if (buttonlist != null && buttonlist.size() > 0) {
            for (SysButton button : buttonlist) {
                if (action.equals(button.getAction())) {
                    return button.getId();
                }
            }
        }
        return null;
    }

    /**
     * 获取指定位数的数据数
     *
     * @param length
     * @return String str
     */
    public static String getRandomStr(Integer length) {
        String str = "";
        for (int i = 0; i < length; i++) {
            str += (int) (Math.random() * 10);
        }
        return str;
    }


    public static boolean isDouble(String s) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        Matcher match = pattern.matcher(s);
        boolean bo = match.matches();
        return bo;
    }

    public static boolean isContains(String key){
        List<String> list = Arrays.asList("25","26","27","28","29","30");
        String substring = key.substring(0, 2);
        boolean contains = list.contains(substring);
        return contains;
    }


    public static void main(String[] args) {
//        System.out.println(getRandomStr(1));
        /*String str1="999999";
        DecimalFormat df=new DecimalFormat("000000");
        String str2=df.format(Integer.parseInt(str1));
        System.out.println(str2);*/
        boolean contains = isContains("28000");
        System.out.println(contains);
    }
}
